<template>
  <el-container class="goods-stock">
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item v-if="modal.params.is_sku == 0" label="商品库存：" prop="add_stock">
          <div class="addstock-title">{{ modal.params.stock }}</div>
          <el-input
            type="number"
            v-model="form.model.add_stock"
            placeholder="补充库存"
            class="addstock-input"
            ><template #append>件</template></el-input
          >
        </el-form-item>
        <!-- 表格 -->
        <div class="sku-table-wrap" v-if="modal.params.is_sku == 1">
          <table class="sku-table" rules="all">
            <thead>
              <tr>
                <th>规格值</th>
                <th>图片</th>
                <th>价格(元)</th>
                <th>划线价格</th>
                <th>成本价</th>
                <th>
                  <div class="sa-flex">
                    <div class="th-title">库存(件)</div>
                    <el-popover
                      placement="top"
                      width="160"
                      trigger="click"
                      v-model:visible="allEditPopover"
                    >
                      <template #reference>
                        <el-icon class="batch-icon"><Edit /></el-icon>
                      </template>
                      <el-input
                        v-model="allEditDatas"
                        placeholder="请输入内容"
                        size="small"
                        class="sa-m-b-10"
                      ></el-input>
                      <div class="sa-flex sa-row-right">
                        <el-button
                          class="is-link"
                          type="primary"
                          size="small"
                          @click="allEditData('cancel')"
                          >取消</el-button
                        >
                        <el-button type="primary" size="small" @click="allEditData('define')"
                          >确定</el-button
                        >
                      </div>
                    </el-popover>
                  </div>
                </th>
                <th>库存预警(件)</th>
                <th>重量(kg)</th>
                <th>规格编码</th>
                <th>商品状态</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, i) in form.model.sku_prices" :key="i">
                <td>{{ item.goods_sku_text.join('/') }}</td>
                <td class="image">
                  <sa-preview :url="item.image" size="28"></sa-preview>
                </td>
                <td>{{ item.price }}</td>
                <td>{{ item.original_price }}</td>
                <td>{{ item.cost_price }}</td>
                <td class="stock">
                  <div class="sa-flex sa-row-between">
                    <div class="stock-title">{{ item.stock }}</div>
                    <el-input
                      v-model="item.add_stock"
                      placeholder="补充库存量"
                      size="small"
                      class="addsku-input"
                      οnkeyup="value=value.replace(/[^\d]/g, '').replace(/^0{1,}/g,'')"
                    ></el-input>
                  </div>
                </td>
                <td class="stock_warning">{{ item.stock_warning }}</td>
                <td>{{ item.weight }}</td>
                <td>{{ item.sn }}</td>
                <td :class="item.status">
                  {{ item.status == 'up' ? `${item.status_text}中` : `已${item.status_text}` }}
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button type="primary" @click="confirm">确 定</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { cloneDeep } from 'lodash';
  import { onMounted, reactive, ref, unref } from 'vue';
  import { api } from '../goods.service';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  const loading = ref(false);
  // 添加 编辑 form
  let formRef = ref(null);
  const form = reactive({
    model: {
      is_sku: 0,
      stock: '',
      add_stock: '',
      sku_prices: [
        {
          goods_sku_text: [],
          id: '',
          add_stock: '',
        },
      ],
    },
    rules: {
      add_stock: [
        { required: true, message: '请输入补充库存', trigger: 'change' },
        {
          pattern: /^(-)?[1-9][0-9]*$/,
          message: '只能输入正整数、负整数',
          trigger: 'change',
        },
      ],
    },
  });
  //input禁止事件
  function handleInput(e) {
    form.model.sku_prices.forEach((i) => {
      i.add_stock = e.target.value.replace(/\D/g, '').replace(/^0{1,}/g, '');
    });
  }

  const allEditPopover = ref(false);
  const allEditDatas = ref('');
  function allEditData(e) {
    if (e == 'cancel') {
      allEditDatas.value = '';
      allEditPopover.value = false;
    } else {
      form.model.sku_prices.forEach((i) => {
        i.add_stock = allEditDatas.value;
      });
      allEditDatas.value = '';
      allEditPopover.value = false;
    }
  }
  //获取多规格数据
  async function getSkuPrice(id) {
    const { data } = await api.goods.getSkuPrice(id);
    form.model.sku_prices = data;
  }
  async function confirm() {
    // 表单验证
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      let submitStock = cloneDeep(form.model.add_stock);
      let list = [];
      form.model.sku_prices.forEach((item) =>
        list.push({ id: item.id, add_stock: item.add_stock }),
      );
      let submitSku = cloneDeep(form.model.sku_prices);
      let submitForm = cloneDeep(form.model);
      const { error } = await api.goods.addStock(props.modal.params.id, submitForm);
      if (error == 0) {
        emit('modalCallBack', {
          event: 'confirm',
        });
      }
    });
  }
  async function init() {
    if (props.modal.params.id) {
      await getSkuPrice(props.modal.params.id);
    }
  }
  onMounted(() => {
    init();
  });
</script>
<style lang="scss" scoped>
  .goods-stock {
    .addstock-title {
      font-size: 12px;
      margin: 0 16px 0 8px;
      line-height: 22px;
      color: var(--sa-font);
    }
    .addstock-input {
      width: 160px;
    }
    .sku-table-wrap {
      width: 100%;
      overflow: auto;
      margin-top: 16px;
      .sku-table {
        width: 100%;
        border: 1px solid var(--sa-border);
        th {
          font-size: 12px;
          color: var(--subtitle);
          height: 32px;
          line-height: 1;
          padding-left: 12px;
          box-sizing: border-box;
          text-align: left;
          .th-title {
            font-size: 12px;
            color: var(--subtitle);
            font-weight: bold;
          }
        }
        td {
          min-width: 88px;
          padding: 0 10px;
          height: 40px;
          box-sizing: border-box;
          &.image {
            min-width: 48px;
          }
          &.stock {
            min-width: 190px;
            .stock-title {
            }
            .addsku-input {
              width: 80px;
            }
          }
          &.stock_warning {
            min-width: 168px;
          }
          &.sn {
            min-width: 116px;
          }
        }
        .up {
          color: var(--el-color-success);
        }
        .down {
          color: var(--el-color-danger);
        }
        .hidden {
          color: var(--el-color-info);
        }
      }
    }
  }
  .batch-icon {
    width: 12px;
    height: 12px;
    margin-left: 10px;
    color: var(--el-color-primary);
  }
</style>
